package com.outsider.product.service;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Page;
import com.outsider.product.dao.Product;
import com.outsider.product.dao.ProductRecord;
import com.outsider.util.AM;
import com.outsider.util.DD;
import com.outsider.util.DownloadUtil;
import com.outsider.util.JD;
import com.outsider.util.OkHttp3;
import com.outsider.weixin.dao.User;

public class ProductService {

    public Page<Product> getPage(int page,int row,Integer userID){
    	return Product.dao.getPage(page, row,userID);
    }
    
    public List<ProductRecord> getLatestRecord(String openID){
    	Integer userID=User.dao.findIDbyOpenID(openID);
    	return ProductRecord.dao.getLatestRecord(userID);
    }
    
    public boolean updateRecord(String openID){
    	List<Product> list=null;
    	if (openID!=null){
    		Integer userID=User.dao.findIDbyOpenID(openID);
        	list=Product.dao.findProductByUserID(userID);
    	}
    	else{
    		list=Product.dao.findAll();
    	}
    	list.forEach(product->{
    		ProductRecord productRecord=new ProductRecord();
    		productRecord.setProductId(product.getID());
    		if (product.getAMURL()!=null&&product.getAMURL().isEmpty()==false){
    			JSONObject result=AM.getProductInfo(product.getAMURL());
    			productRecord.setAmPrice(new BigDecimal(result.getDouble("price")));
    		}
    		if (product.getDDURL()!=null&&product.getDDURL().isEmpty()==false){
            	JSONObject result=DD.getProductInfo(product.getDDURL());
            	productRecord.setDdPrice(new BigDecimal(result.getDouble("price")));
            }
            if (product.getJDURL()!=null&&product.getJDURL().isEmpty()==false){
            	JSONObject result=JD.getProductInfo(product.getJDURL());
            	productRecord.setJdPrice(new BigDecimal(result.getDouble("price")));
            }
            productRecord.setDATE(new Date());
            productRecord.save();
    	});
    	return true;
    }

	public boolean addProduct(String name,String AMURL, String DDURL, String JDURL, Integer userID) {
        Product product=new Product();
        product.setNAME(name);
        ProductRecord productRecord=new ProductRecord();
        String picurl=null;
        product.setUserId(userID);
        if (AMURL!=null){
        	product.setAMURL(AMURL);
        	JSONObject result=AM.getProductInfo(AMURL);
        	product.setAMCODE(result.getString("code"));
        	productRecord.setAmPrice(new BigDecimal(result.getDouble("price")));
        	if (picurl==null) picurl=AM.getProductPic(AMURL);
        }
        if (DDURL!=null){
        	product.setDDURL(DDURL);
        	JSONObject result=DD.getProductInfo(DDURL);
        	product.setDDCODE(result.getString("code"));
        	productRecord.setDdPrice(new BigDecimal(result.getDouble("price")));
        	if (picurl==null) picurl=DD.getProductPic(DDURL);
        }
        if (JDURL!=null){
        	product.setJDURL(JDURL);
        	JSONObject result=JD.getProductInfo(JDURL);
        	product.setJDCODE(result.getString("code"));
        	productRecord.setJdPrice(new BigDecimal(result.getDouble("price")));
        	if (picurl==null) picurl=JD.getProductPic(JDURL);
        }
        Integer ID=Product.dao.getLastestID();
        DownloadUtil.downLoadFromUrl(picurl, ID+".jpg", PathKit.getWebRootPath()+File.separator+"product"+File.separator+"picture");
        product.setPIC(ID+".jpg");
        product.save();
        productRecord.setProductId(ID);
        productRecord.setDATE(new Date());
        productRecord.save();
        return true;
	}

}
